﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
///保存浏览者信息、实现浏览者操作的类
/// </summary>
public class UserInfo
{
    private String userID = null;//用户ID，即登陆名。从外部数据库读入
    private String nickname = null;//昵称
    private int popedom =0;//权限，判断上传权限。
    //预定义权限
    //static public  const int STUDENT=0;
    //static public  const int TEACHER=5;

    //出于安全考虑，我觉得密码不能从数据库中读取，删除了password的setter
    //修改者：徐家兴，2009.09.12
    private String password = null;
   
 
    public UserInfo(){}

    /// <summary>
    /// 向数据库添加用户信息
    /// </summary>
    /// <param name="userID"></param>
    /// <param name="nickname"></param>
    /// <param name="password"></param>
    /// <param name="popedom"></param>
    /// <returns></returns>
    public bool Add(String userID,String nickname,String password,int popedom)
    {
        this.userID = userID;
        this.nickname = nickname;
        this.password = password;
        this.popedom = popedom;

        String sql = "insert into tb_UserInfo(userID,nickname,password,popedom) values('"+userID+"','"+nickname+"','"+password+"',"+popedom.ToString()+")";
        SqlExecutor myExec = new SqlExecutor();
        return myExec.UpdateCmd(sql);
    }

    /// <summary>
    ///  向数据库修改用户信息
    /// </summary>
    /// <param name="userID"></param>
    /// <param name="nickname"></param>
    /// <param name="password"></param>
    /// <param name="popedom"></param>
    /// <returns></returns>
    public bool Update(String userID, String nickname, String password, int popedom)
    {
        this.userID = userID;
        this.nickname = nickname;
        this.password = password;
        this.popedom = popedom;

        bool flag = true;//标记判断是否第一个更新属性
        String sql = "update tb_UserInfo set ";
        if (nickname != null)
        {
            if (flag)
            {
                sql += "nickname='" + nickname + "'";
                flag = false;
            }
            else
            {
                sql += "," + "nickname='" + nickname + "'";
            }
        }
        if (password != null)
        {
            if (flag)
            {
                sql += "password='" + password + "'";
                flag = false;
            }
            else
            {
                sql += "," + "password='" + password + "'";
            }
        }
        if (popedom >= 0)
        {
            if (flag)
            {
                sql += "popedom=" + popedom.ToString();
                flag = false;
            }
            else
            {
                sql += "," + "popedom=" + popedom.ToString();
            }
        }
        sql+= " where userID='"+userID+"'";
        SqlExecutor myExec = new SqlExecutor();
        return myExec.UpdateCmd(sql);
    }

    /// <summary>
    /// 删除用户在数据库中的记录
    /// </summary>
    /// <param name="userID"></param>
    /// <returns></returns>
    public bool Delete(String userID)
    {
        this.userID = userID;
        String sql = "delete from tb_UserInfo where userID='" + userID + "'";
        SqlExecutor myExec = new SqlExecutor();
        return myExec.UpdateCmd(sql);
    }

    //有待考虑
    public DataTable select(String userID)
    {
        this.userID = userID;
        String sql = "select * from tb_UserInfo where userID='" + userID + "'";
        SqlExecutor myExec = new SqlExecutor();
        return myExec.SelectCmd(sql);
    }

    /// <summary>
    /// 从数据库中读取用户信息
    /// </summary>
    /// <returns></returns>
    public bool Load()
    {
        String sql = "select * from tb_UserInfo where userID='" + userID + "'";
        SqlExecutor myExec = new SqlExecutor();
        DataTable userInfo = myExec.SelectCmd(sql);
        if (userInfo.Rows.Count == 0) return false;
        nickname = (string)userInfo.Rows[0]["nickname"];
        popedom = (int)userInfo.Rows[0]["popedom"];
        password = (string)userInfo.Rows[0]["password"];
        return true;
    }

    public String UserID
    {
        get { return userID; }
        set { userID = value; }
    }

    public String Nickname
    {
        get { return nickname; }
        set { nickname = value; }
    }

    public int Popedom
    {
        get { return popedom; }
        set { popedom = value; }
    }

    public String Password
    {
        get { return password; }
        set { password = value; }
    }


}

